package com.xiao.sulinguser.sms;

import com.xiao.sulinguser.bindproperties.SmsProperties;
import com.xiao.sulinguser.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 1322127758@qq.com
 * @date: 2022/3/13 9:28
 * @作用: 发送短信
 */
@Component
public class Sms {

    @Autowired
    private SmsProperties smsProperties;

    /**
     * 发送短信的函数
     * @param mobile :要发送给哪个手机号码
     * @param code   :要发送的验证码
     */
    public void sendCode(String mobile,String code){
/*      原内容
        String host = "http://dingxin.market.alicloudapi.com";*/
        String host = smsProperties.getHost();
        String path = "/dx/sendSms";
        String method = "POST";
//        String appcode = "你自己的AppCode";
        String appcode = smsProperties.getAppcode();
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        //要往哪个手机号发送短信
//        querys.put("mobile", "159xxxx9999");
        querys.put("mobile", mobile);
        //验证码值 (自定义的)
//        querys.put("param", "code:"+"1234");
        querys.put("param", "code:"+code);
        //哪个公司名对应的id, TP1711063
        querys.put("tpl_id", smsProperties.getTplId());
        Map<String, String> bodys = new HashMap<String, String>();


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
